package com.feeyo.groundservice.vehiclemonitorserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.OperatorImportFromGeoJson;
import com.esri.core.geometry.WktImportFlags;
import com.feeyo.groundservice.base.rdb.PostgresDb;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class CrossingManager extends AbstractVerticle {
Logger logger = LoggerFactory.getLogger(LimitAreaManager.class);
	
    public static final Map<String, List<Crossing>> lines = new HashMap<String, List<Crossing>>();

    @Override
    public void start(Future<Void> startFuture) throws Exception {
    	try{
    	
	        PostgresDb.postgreSQLClient.getConnection(getConn -> {
	            if (getConn.failed()){
	                System.out.println(getConn.cause());
	                return;
	            }
	            if(getConn.succeeded()){
		            getConn.result().query("select id,name,ST_AsGeoJSON(line) as line, power,airport from vehiclemonitor.crossing where not _deleted", ar->{
		                getConn.result().close();
		                if(ar.succeeded()){
		                    lines.clear();
		                    int numRows = ar.result().getNumRows();
		                    for(int i = 0; i < numRows; i++){
		                    	Crossing crossing = new Crossing();
		                    	//System.out.println("crossing->"+ar.result().getRows().get(i).getString("line"));
		                        MapGeometry line = OperatorImportFromGeoJson.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polyline, ar.result().getRows().get(i).getString("line"), null);
		                        String airport = ar.result().getRows().get(i).getString("airport");
		                        if (!lines.containsKey(airport)){
		                        	lines.put(airport, new ArrayList<Crossing>());
		                        }
		                        try{
			                        crossing.setId(ar.result().getRows().get(i).getString("id"));
		                        }catch(Exception e){
		                        	e.printStackTrace();
		                        }
		                        //crossing.setPower();
		                        if (ar.result().getRows().get(i).getString("power") == null)
		                        	continue;
		                        logger.info("load lines->" + crossing.getId());
		                        JsonArray powerArr= new JsonArray(ar.result().getRows().get(i).getString("power"));
		                        Iterator<Object> iter = powerArr.iterator();
		                        while(iter.hasNext()){
		                        	JsonObject item = (JsonObject)iter.next();
		                        	CrossingPower power= new CrossingPower();
		                        	power.setDepts(item.getString("depts"));
		                        	power.setTypes(item.getString("types"));
		                        	crossing.getPowers().add(power);
		                        }
		                        crossing.setLine(line);
		                        lines.get(airport).add(crossing);	
		                    }
		                }
		                
		                startFuture.complete();
		            });//
	            }
	        });
        
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
}
